package com.java.base.exercise.designmode.responsibilitychain;

import org.apache.commons.math3.analysis.function.Abs;

/**
 * @Author RenPu
 * @Date 2024/2/3 18:09
 * @Version 1.0
 * @Description: 使用链表的形式 实现责任链
 **/
public class ResponsibilityLinkedChain {


    /**
     * 构建一个链表的头节点
     */

   public HandlerContextChain head= new HandlerContextChain(new AbstractHandler() {
        @Override
        void doHandler(HandlerContextChain handlerContextChain, String arg0) {
            handlerContextChain.runNext(arg0);
        }
    });


    public void requestProcess(String arg0){
        this.head.handler(arg0);
    }

    public void addLast(AbstractHandler abstractHandler){
        HandlerContextChain context =this.head;
        while (context.next!=null){
            context=context.next;
        }
        context.next=new HandlerContextChain(abstractHandler);
    }



    /**
     * 抽象公共接口方法
     */
   static abstract class AbstractHandler{
      abstract void doHandler( HandlerContextChain handlerContextChain,String arg0);

    }


    /**
     * 维护链表的上下文
     */
    class  HandlerContextChain{

        private AbstractHandler handler;
        private HandlerContextChain next;

        public HandlerContextChain(AbstractHandler handler) {
            this.handler = handler;
        }

        public void handler(String arg0){
            this.handler.doHandler(this,arg0);
        }

        public void runNext(String arg0) {
          if (this.next!=null){
             next.handler(arg0);
          }
        }
    }

    /**
     * 具体责任链实现逻辑 实例0
     */
    static class Handler0 extends AbstractHandler{

        @Override
        void doHandler(HandlerContextChain handlerContextChain, String arg0) {
            arg0=arg0+"...Handler 00000的小尾巴......";

            System.out.println(
                    "我是Handler---00000实例，我正在处理：："+arg0
            );

            handlerContextChain.runNext(arg0);
        }
    }



    /**
     * 具体责任链实现逻辑 实例1
     */
    static class Handler1 extends AbstractHandler{

      @Override
      void doHandler(HandlerContextChain handlerContextChain, String arg0) {
         arg0=arg0+"...Handler 1111111的小尾巴......";

          System.out.println(
                  "我是Handler111111实例，我正在处理：："+arg0
          );

          handlerContextChain.runNext(arg0);
      }
  }


    /**
     * 具体责任链实现逻辑 实例2
     */
    static class Handler2 extends AbstractHandler{

        @Override
        void doHandler(HandlerContextChain handlerContextChain, String arg0) {
            arg0=arg0+"...Handler 2222222的小尾巴......";

            System.out.println(
                    "我是Handler222222实例，我正在处理：："+arg0
            );

        }
    }


    public static void main(String[] args) {

        ResponsibilityLinkedChain linkedChain = new ResponsibilityLinkedChain();
        linkedChain.addLast(new Handler1());
        linkedChain.addLast(new Handler2());

        //发起请求

        linkedChain.requestProcess("我是一列小火车 呜呜呜 ~~~~~~~~~~~");


    }



}
